home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MISC.SWG / 0073_Eight Queens.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  5KB  |  204 lines

  1. {
  2. pigeons@JSP.UMontreal.CA (Pigeon Steven)
  3.  
  4. >     Hey, I have a friend who is taking a Pascal class at another col-
  5. >lege and he asked me to make a query of you all.  Basically, he has to
  6. >do the "eight queens" on a chessboard (with none of them interfering
  7. >vertically, horizontally, or diagonally with each other) problem in
  8. >Pascal.  The program has to use stacks.  Its input is the number of
  9. >queens (the dimensions of the chessboard are that number x that number).
  10. >The output is that it can't be done with that number of queens or a
  11. >grid of the queens and either empty spaces or dashes.  I was wondering
  12. >if any of you had any similar programs in old code lying around, and if
  13. >so if you could send it to me.  My friend says it's a pretty classic
  14. >problem for programmers, so I figured I'd ask.  Oh, and in case some of
  15. >you think that I am this "friend", the only Pascal course here at Brown
  16. >(cs15) has already done its job with stacks, and it wasn't this.  Btw,
  17. >speaking of cs here, it's Object-Oriented; my friend's program needs to
  18. >be done procedureally (straight-line), not in OOPas.  I thank you all
  19. >for your indulgence in allowing me to post this.  Please don't flame me,
  20. >as I am only trying to help out a friend.  If there is a more appropriate
  21. >place for me to post this, please tell me (I am going to post this to
  22. >cs groups if possible).  Oh, and as I don't get around here often, I
  23. >would appreciate it much if any and all replies were sent to the address
  24. >below.  Thanx,
  25. >
  26.  
  27. Here's a programm that does that. It's a little bit strange, but I put
  28. extra code so the board would not be passed as a parameter (since Turbo
  29. Profiler said :"Hey, 75% of your run time goes in copy of the board").
  30. The file is name REINES5.PAS (litterally QUEENS5.PAS) and it's limited
  31. (so to say) to 64x64 boards (with 64 queens on it). It is fast enough.
  32.  
  33.  
  34. }
  35.  program Probleme_des_reines;
  36.  
  37.  const max = 64;
  38.        libre = 8;
  39.        reine = 8;
  40.  
  41.  const colname:string =
  42.                         'abcdefghijklmnopqrstuvwxyz'+
  43.                         'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+
  44.                         'αßΓτΣσµΦΘΩδ';
  45.  type echiquier = array[1..max,1..max] of byte;
  46.  var  sol,recursions:longint;
  47.       top:word;
  48.       Reines,Attaques:echiquier;
  49.  
  50.  
  51.  function min(a,b:integer):integer;
  52.   begin
  53.    if a<b
  54.       then min:=a
  55.       else min:=b;
  56.   end;
  57.  
  58.  procedure mark(x,y:integer);
  59.  var t,g,i:integer;
  60.   begin
  61.    for t:=y+1 to top do inc(attaques[x,t]);
  62.  
  63.    t:=x+1;
  64.    g:=y+1;
  65.  
  66.    for i:=1 to min(top-t,top-g)+1 do
  67.     begin
  68.      inc(attaques[t,g]);
  69.      inc(t);
  70.      inc(g);
  71.     end;
  72.  
  73.    t:=x-1;
  74.    g:=y+1;
  75.  
  76.    if t>0 then
  77.    for i:=1 to min(top-g+1,t) do
  78.     begin
  79.      inc(attaques[t,g]);
  80.      dec(t);
  81.      inc(g);
  82.     end;
  83.  
  84.    Reines[x,y]:=reine;
  85.  
  86.   end;
  87.  
  88.  procedure unmark(x,y:integer);
  89.  var t,g,i:integer;
  90.   begin
  91.    for t:=y+1 to top do dec(attaques[x,t]);
  92.  
  93.    t:=x+1;
  94.    g:=y+1;
  95.  
  96.    for i:=1 to min(top-t,top-g)+1 do
  97.     begin
  98.      dec(attaques[t,g]);
  99.      inc(t);
  100.      inc(g);
  101.     end;
  102.  
  103.  
  104.    t:=x-1;
  105.    g:=y+1;
  106.  
  107.    if t>0 then
  108.    for i:=1 to min(top-g+1,t) do
  109.     begin
  110.      dec(attaques[t,g]);
  111.      dec(t);
  112.      inc(g);
  113.     end;
  114.  
  115.    Reines[x,y]:=libre;
  116.  
  117.   end;
  118.  
  119.  
  120.  
  121.  procedure traduit;
  122.  var t,g:integer;
  123.   begin
  124.    write(sol:4,'. ');
  125.    for t:=1 to top do
  126.     for g:=1 to top do
  127.      if Reines[g,t]=reine then write(colname[t],g,' ');
  128.    writeln('  ',recursions);
  129.   end;
  130.  
  131.  
  132.  function find(level,j:integer):integer;
  133.   begin
  134.    inc(j);
  135.    while (attaques[j,level]<>libre) and (j<top) do inc(j);
  136.    if (attaques[j,level]=libre)
  137.       then find:=j
  138.       else find:=0;
  139.   end;
  140.  
  141.  
  142.  
  143.  procedure recurse(level:integer);
  144.  var t:integer;
  145.   begin
  146.    inc(recursions);
  147.    t:=0;
  148.    repeat
  149.     t:=find(level,t);
  150.     if t<>0
  151.        then begin
  152.              if level=top
  153.                 then begin
  154.                       inc(sol);
  155.                       Reines[t,level]:=reine;
  156.                       traduit;
  157.                       Reines[t,level]:=libre;
  158.                      end
  159.                 else begin
  160.                       mark(t,level);
  161.                       recurse(level+1);
  162.                       unmark(t,level);
  163.                      end;
  164.             end
  165.    until (t=0) or (t=top);
  166.   end;
  167.  
  168.  
  169.   function fact(n:real):real;
  170.    begin
  171.     if n<=1 then fact:=1
  172.             else fact:=n*fact(n-1);
  173.    end;
  174.  
  175.  
  176.  var a:echiquier;
  177.      i:integer;
  178.  begin
  179.  
  180.  
  181.   sol:=0;
  182.   val(paramstr(1),top,i);
  183.   if top>max
  184.      then begin
  185.            writeln('! ',Top,' a ete remis a ',max,' (max)');
  186.            top:=max;
  187.           end;
  188.  
  189.   if top<1 then top:=1;
  190.  
  191.   writeln;
  192.   writeln(' Le probleme des ',top,' reines FAST (c) 1992-1993 Steven Pigeon');
  193.   writeln;
  194.  
  195.   recursions:=0;
  196.   fillchar(attaques,sizeof(attaques),libre);
  197.   fillchar(Reines,sizeof(Reines),libre);
  198.   recurse(1);
  199.   writeln;
  200.   writeln(' Solutions: ',sol);
  201.   writeln(' Recursions: ',recursions,' (au lieu de ',fact(top):0:0,')');
  202.  end.
  203.  
  204.